﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;

namespace MVVMSuite.Model
{
    public class Property : ObservableObject
    {
        // Fields...
        private bool _HaveBackStore;
        private bool _IsReadOnly;
        private bool _NotifyPropertyChanged;
        private string _Description;
        private string _Type;
        private string _Name;


        public string Name
        {
            get { return _Name; }
            set
            {
                if (_Name == value)
                    return;
                _Name = value;
                RaisePropertyChanged("Name");
            }
        }


        public string Type
        {
            get { return _Type; }
            set
            {
                if (_Type == value)
                    return;
                _Type = value;
                RaisePropertyChanged("Type");
            }
        }


        public string Description
        {
            get { return _Description; }
            set
            {
                if (_Description == value)
                    return;
                _Description = value;
                RaisePropertyChanged("Description");
            }
        }


        public bool NotifyPropertyChanged
        {
            get { return _NotifyPropertyChanged; }
            set
            {
                if (_NotifyPropertyChanged == value)
                    return;
                _NotifyPropertyChanged = value;
                RaisePropertyChanged("NotifyPropertyChanged");
            }
        }

        public bool IsReadOnly
        {
            get { return _IsReadOnly; }
            set
            {
                if (_IsReadOnly == value)
                    return;
                _IsReadOnly = value;
                RaisePropertyChanged("IsReadOnly");
            }
        }

                
        public bool IsAutoImplemented
        {
            get { return _HaveBackStore; }
            set
            {
                if (_HaveBackStore == value)
                    return;
                _HaveBackStore = value;
                RaisePropertyChanged("IsAutoImplemented");
            }
        }

        public Property(string name, string type, bool isAutoImplemented = true, bool notifyPropertyChanged = true, bool isReadOnly = false, string description = "")
        {
            Name = name;
            Type = type;
            IsAutoImplemented = isAutoImplemented;
            NotifyPropertyChanged = notifyPropertyChanged;
            IsReadOnly = isReadOnly;
            Description = description;
        }

        public string AsCSharpCode
        {
            get
            {
                if (IsAutoImplemented)
                {
                    if (IsReadOnly)
                    {
                        return String.Format("/* {2} */\npublic {0} {1} { get; }", Type, Name, Description);
                    }
                    else
                    {
                        return String.Format("/* {2} */\npublic {0} {1} {{ get; set; }}", Type, Name, Description);
                    }
                }
                else
                {
                    StringBuilder s = new StringBuilder();
                    s.AppendLine(String.Format("/* {0} */", Description));
                    s.AppendLine(String.Format("private {0} _{1};", Type, Name));
                    s.AppendLine(String.Format("public {0} {1}", Type, Name));
                    s.AppendLine("{");
                    s.AppendLine(String.Format("\tget {{ return _{0}; }}", Name));
                    if (IsReadOnly == false)
                    {
                        s.Append(String.Format("\tset {{ _{0} = value; ", Name));
                        if (NotifyPropertyChanged)
                        {
                            s.AppendLine(String.Format(" RaisePropertyChanged(\"{0}\"); }}", Name));
                        }
                        else
                        {
                            s.AppendLine(" }\n");
                        }
                    }
                    s.AppendLine("}");
                    return s.ToString();
                }
            }
        }

    }
}
